<?php


namespace app\admin\controller;


class Image extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index(){
        $list = db('config')->column('value', 'name');
        if(request()->isPost()){
            $param = input('post.');
            if(empty($param)){
                $this->error('数据有误！');
            }
            foreach ($param as $key=>$item){
                $item=trim($item);
                if(!empty($item) && !$this->check_url($item)){
                    $this->error('请检查输入的图片网址是否正确,错误的图片网址是'.$item);
                }
                if(isset($list[$key])){
                    db('config')->where('name',$key)->update(['value'=>$item]);
                }else{
                    db('config')->insertGetId(['value'=>$item,'name'=>$key]);
                }

            }

            $this->success('操作成功！');

        }else {

            $this->assign($list);
            $types = db('zhibo_type')->order('sort', 'asc')->select();
            $this->assign('types', $types);
            return $this->fetch('admin@image/index');
        }
    }

    public function check_url($url){
        $result = strpos($url,'https://')===0 || strpos($url, 'http://')===0;
        return $result;
    }
}